<?php

namespace app\domain\admin;

use app\model\admin\AdminGroup;
use app\model\admin\AdminGroupMenusRelation;
use app\model\admin\AdminMenus;
use think\captcha\facade\Captcha;

class CommonDomain
{
    public string $message = 'success';

    public function getCaptcha(): array
    {
        try {
            $captcha = Captcha::create('verify');
            $captcha = base64_encode($captcha->getData());
            return ['img_base64' => "data:image/jpeg;base64,{$captcha}"];
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            return [];
        }
    }

    public function adminMenus($type): array
    {
        try {
            if ('all' == $type) {
                $list = app(AdminMenus::class)->field(['*', 'title' => 'label', 'id' => 'key'])->order('sort ASC,id ASC')->select();
            } else {
                $groupId = getUserInfo()['group_id'] ?? null;
                $relations = app(AdminGroupMenusRelation::class)->where(['group_id' => $groupId])->column('menu_id');
                $list = app(AdminMenus::class)->whereIn('id', $relations)->order('sort ASC,id ASC')->select();
            }

            $list = commonRecursion($list, 0, 0, 'pid', 'children', true);

            return [
                'list' => $list
            ];
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            return [];
        }
    }

    public function adminGroup(): array
    {
        try {
            $list = app(AdminGroup::class)->select();

            return [
                'list' => $list
            ];
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            return [];
        }
    }

    public function getMenuList(): array
    {
        try {
            $groupId = getUserInfo()['group_id'] ?? null;
            $menuIds = app(AdminGroupMenusRelation::class)->where(['group_id' => $groupId])->column('menu_id');
            $list = app(AdminMenus::class)
                ->whereIn('id', $menuIds)
                ->field(['*', 'id' => 'key', 'title' => 'label'])
                ->order('sort ASC,id DESC')
                ->select();
            $result = [];
            $list->filter(function ($row) use (&$result) {
                $result[] = [
                    'id' => $row['id'],
                    'pid' => $row['pid'],
                    'path' => $row['route'],
                    'name' => $row['component_name'],
                    'component' => 0 === $row['pid'] ? 'LAYOUT' : $row['component'],
                    'redirect' => $row['redirect'],
                    'meta' => [
                        'id' => $row['id'],
                        'title' => $row['title'],
                        'icon' => $row['icon'],
                    ]
                ];
            });

            return commonRecursion($result, 0, 0, 'pid', 'children', false);
        } catch (\Exception $e) {
            $this->message = $e->getMessage();
            return [];
        }
    }
}